package org.hackystat.devcathlon.ui.page.matches.invitations;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.team.TeamManager;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.matches.MatchesSession;

/**
 * Panel that displays all the invitations the user has yet to accept.
 * 
 * @author Ka Hung Phillip Lau
 * 
 */
public class InvitationListPanel extends Panel {

  /**
   * Support serialization.
   */
  private static final long serialVersionUID = 1L;

  /**
   * Constructs the Invitation List panel with the wicket id.
   * 
   * @param id The wicket name id.
   */
  public InvitationListPanel(String id) {
    super(id);

    List<Match> invites = getPendingInvites();

    System.out.println(invites);
    // display a message if there are no invites
    String message = "";
    if (invites.isEmpty()) {
      message = "You currently have no invites.";
      DevcathlonSession.get().getMatchesSession().clearSelectedMatch();
    }

    add(new Label("InviteMessage", message));

    ListView listInvites = new ListView("ListInvites", 
        MatchesSession.convertToMatchNames(invites)) {

      private static final long serialVersionUID = 1L;

      @Override
      protected void populateItem(ListItem item) {
        final String matchName = (String) item.getModelObject();
        Link matchLink = new Link("InvitationLink") {
          private static final long serialVersionUID = 1L;

          @Override
          public void onClick() {
            //Match match = MatchManager.getInstance().getMatch(matchName);
            DevcathlonSession.get().getMatchesSession().setSelectedMatch(matchName);
            
            setResponsePage(InvitationsPage.class);
          }
        };
        Match match = MatchManager.getInstance().getMatch(matchName);
        matchLink.add(new Label("MatchName", match.getName()));
        item.add(matchLink);
      }
    };
    add(listInvites);
  }

  /**
   * Retrieves all matches the user was invited to.
   * 
   * @return The list of teams still pending an answer.
   */
  private List<Match> getPendingInvites() {

    List<Match> matchesInvitedTo = new ArrayList<Match>();
    String user = DevcathlonSession.get().getEmail();
    Set<Team> userTeams = new HashSet<Team>(TeamManager.getInstance().getOwnedTeams(user));
    for (Match match : MatchManager.getInstance().getInvolvedMatches(user)) {
      for (Team team : match.getTeams()) {
        if (userTeams.contains(team) && (match.getInvitationStatus(team) == null)) {
          matchesInvitedTo.add(match);
        }
      }
    }
    return matchesInvitedTo;
  }

}
